package mscontrol;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.dao.TeacherDao;
import model.entity.Teacher;

/**
 * Servlet implementation class GetMSTeacher
 */
@WebServlet("/GetMSTeacher")
public class GetMSTeacher extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetMSTeacher() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		 Enumeration<String> teacherid=request.getParameterNames();
		 String condit=(String)request.getParameter("condit");
 
		 Teacher teacher=new Teacher();
		 
		 for(int i=0;i<5;i++){
			 String paraName=(String)teacherid.nextElement();
			 String paraVal=request.getParameter(paraName);
			 System.out.println(paraName + " " + paraVal);
			 if(paraVal!=""){
			     switch (i) {
				case 0:
					teacher.setId(Integer.valueOf(paraVal));
					break;
                case 1:
					teacher.setName(paraVal);
					break;
                case 2:
					teacher.setSex(paraVal);
					break;
                case 3:
	                teacher.setAge(Integer.valueOf(paraVal));
	                break;
                case 4:
	                teacher.setDuty(paraVal);
	                break;
				default:
					break;
				}
			 }
		 }
		 
		 TeacherDao td=new TeacherDao();
		 List<Teacher> list=td.getRanSelect(teacher, condit);
         
		 String str="[";
		 for (Teacher tea : list) {
			str+="{\"ID\":" + tea.getId() + ",\"Name\":\"" + tea.getName() + "\",\"Sex\":\"" + tea.getSex()
		          + "\",\"Age\":" + tea.getAge() + ",\"Duty\":\"" + tea.getDuty() + "\"},";
		 }
		 
		 str=str.substring(0, str.length()-1);
		 str+="]";
		 response.setCharacterEncoding("utf-8");
		 PrintWriter out=response.getWriter();
		 out.print(str);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
		
	}

}
